package com.uchengwang.ui.cm_ui.uu.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.me.bean.UniFeedbackBean;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUniManager;
import com.uchengwang.ui.cm_ui.me.presenter.HttpUserManager;
import com.uchengwang.ui.cm_ui.uu.adapter.CmtmsgAdapter;
import com.uchengwang.ui.cm_ui.uu.adapter.FeedbackResponseAdapter;
import com.uchengwang.ui.cm_ui.uu.adapter.MyNoticeAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.CmtmsgBean;
import com.uchengwang.ui.cm_ui.uu.bean.NoticeBean;
import com.uchengwang.ui.cm_ui.uu.http.UUHttpManager;

import java.util.List;

/**
 * Created by U城网 on 2017/8/17.
 * U城对发布的信息的评论/反馈
 */

public class CmtMsgAndFeedbackActivity extends BaseWithTitleActivity{

    private PtrClassicFrameLayout layout;
    private RecyclerAdapterWithHF mAdapter;
    private RecyclerView invite_rv;
    private CmtmsgAdapter adapter;
    private FeedbackResponseAdapter feedbakAdapter;
//    private SystemFeedbackAdapter systemFeedbackAdapter;
    private MyNoticeAdapter noticeAdapter;

    private int type;//1获取评论  2获取反馈[普通用户] 3uni用户获取反馈

    private int page = 1, totalPage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_group_invite);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 0);
        setTitle("Uni团队");


        initView();
    }

    private void initView(){
        layout = (PtrClassicFrameLayout) findViewById(R.id.invite_layout);
        invite_rv = (RecyclerView) findViewById(R.id.invite_rv);

        initData();
    }

    private void initData(){
        invite_rv.setLayoutManager(new LinearLayoutManager(this));
        switch (type){
            case 1://获取评论
//                setTitle("系统通知");
                adapter = new CmtmsgAdapter(this);
                mAdapter = new RecyclerAdapterWithHF(adapter);

//                invite_rv.setAdapter(mAdapter);
//                layout.setAutoLoadMoreEnable(true);
//                layout.setLoadMoreEnable(true);
                break;
            case 2://反馈[普通用户]
//                setTitle("系统通知");
                noticeAdapter = new MyNoticeAdapter(this);
                mAdapter = new RecyclerAdapterWithHF(noticeAdapter);
//                invite_rv.setAdapter(mAdapter);
//                layout.setAutoLoadMoreEnable(true);
//                layout.setLoadMoreEnable(true);
                break;
            case 3://反馈[uni]
//                setTitle("系统通知");
                feedbakAdapter = new FeedbackResponseAdapter(this, 2);
                mAdapter = new RecyclerAdapterWithHF(feedbakAdapter);

//                invite_rv.setAdapter(mAdapter);
//                layout.setAutoLoadMoreEnable(true);
//                layout.setLoadMoreEnable(true);
                break;
        }

        invite_rv.setAdapter(mAdapter);
        layout.setAutoLoadMoreEnable(true);
        layout.setLoadMoreEnable(true);



        layout.postDelayed(new Runnable() {

            @Override
            public void run() {
                layout.autoRefresh(true);
            }
        }, 150);

        layout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;

//                switch (type){
//                    case 1:
//                    case 3:
//                        layout.loadMoreComplete(true);
//                        break;
//                }
                getHttps(true);
            }
        });

        layout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totalPage){
                    //当前请求页大于总页数，不再请求
                    getHttps(false);
                } else {
                    layout.loadMoreComplete(false);
//                    switch (type){
//                        case 1:
//                        case 3:
//                            layout.loadMoreComplete(false);
//                            break;
//                    }
                }
            }
        });
    }

    /**
     * 获取评论的消息
     * */
    private void getHttps(final boolean isRefresh){

        switch (type){
            case 1://获取评论
                UUHttpManager.getManager().getCmtMsg(this, page, 10, true, new UUHttpManager.CmtMsgListener() {
                    @Override
                    public void onResult(int stutas, int totalPages, List<CmtmsgBean> listBean) {
                        totalPage = totalPages;
                        if (isRefresh){
                            layout.refreshComplete();
                        }else {
                            layout.loadMoreComplete(true);
                        }

                        if (stutas ==0 && listBean != null){
                            if (isRefresh){
                                adapter.setData(listBean);
                            }else {
                                adapter.getData().addAll(listBean);
                                adapter.notifyDataSetChanged();
                            }
                        }
                    }
                });
                break;
            case 2://反馈 普通用户
                HttpUserManager.getManager().getMyNotice(this, page, 10, null, true, new OnHttpResultListener<NoticeBean>() {
                    @Override
                    public void onResult(int status, String msg, int total, List<NoticeBean> list) {
                        if (isRefresh){
                            layout.refreshComplete();
                        }else {
                            layout.loadMoreComplete(true);
                        }

                        if (status ==0 && list != null){
                            if (isRefresh){
                                noticeAdapter.setData(list);
                            }else {
                                noticeAdapter.getData().addAll(list);
                                noticeAdapter.notifyDataSetChanged();
                            }
                        }
                    }
                });

//                HttpMeManager.getPresenter().getReleaseFeedback(this, page, new OnHttpResultListener<UcityInfoBean>() {
//                    @Override
//                    public void onResult(int status, String msg, int total, List<UcityInfoBean> list) {
//                        if (isRefresh){
//                            layout.refreshComplete();
//                        }else {
//                            layout.loadMoreComplete(true);
//                        }
//
//                        if (status ==0 && list != null){
//                            if (isRefresh){
//                                systemFeedbackAdapter.setData(list);
//                            }else {
//                                systemFeedbackAdapter.getData().addAll(list);
//                                systemFeedbackAdapter.notifyDataSetChanged();
//                            }
//                        }
//                    }
//                });

//                UUHttpManager.getManager().getFeedbacks(this, new OnResultListener() {
//                    @Override
//                    public void onResult(int stutas, String msg, int total, String data) {
//                        if (isRefresh){
//                            layout.refreshComplete();
//                        }else {
//                            switch (type){
//                                case 1:
//                                    layout.loadMoreComplete(true);
//                                    break;
//                            }
//                        }
//
//                        if (stutas ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
//                            List<FeedbackBean> list = JsonUtils.getManager().parseList(data, FeedbackBean.class);
//                            feedbakAdapter = new FeedbackResponseAdapter(CmtMsgAndFeedbackActivity.this, list, 1);
//                            invite_rv.setAdapter(feedbakAdapter);
//                        }
//                    }
//                });
                break;
            case 3://uni用户
                HttpUniManager.getManager().getFeedback(this, page, 10, new HttpUniManager.UniFeedbackListener() {
                    @Override
                    public void onResult(int stutas, String msg, int totalPages, List<UniFeedbackBean> list) {
                        totalPage = totalPages;
                        if (isRefresh){
                            layout.refreshComplete();
                            feedbakAdapter.setData(list);
                        }else {
                            layout.loadMoreComplete(true);

                            feedbakAdapter.getData().addAll(list);
                            feedbakAdapter.notifyDataSetChanged();
                        }
                    }
                });

                break;
        }

    }
}
